﻿using System;
using System.Collections.Generic;
using System.IO;
using Platformer_Example.Controller.Character;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace Platformer_Example.Controller.Map
{
    public class MapPlayerData
    {
        public string MapName { get; private set; }
        public CharacterPosition NewPosition { get; private set; }

        public MapPlayerData(string mapName, TE_Vector2 positionGridBound, BaseInputCharacter.State state,
                             BaseInputCharacter.Direction direction)
        {
            MapName = mapName;
            NewPosition = new CharacterPosition
                              {
                                  Position = positionGridBound,
                                  Direction = direction,
                                  State = state
                              };
        }

        public MapPlayerData(string description)
        {
            var values = description.Split(' ');
            MapName = values[0];
            NewPosition = new CharacterPosition
                              {
                                  Position =
                                      new TE_Vector2(Convert.ToSingle(values[1]),
                                                     Convert.ToSingle(values[2])),
                                  State =
                                      (BaseInputCharacter.State)
                                      Enum.Parse(typeof (BaseInputCharacter.State), values[3]),
                                  Direction =
                                      (BaseInputCharacter.Direction)
                                      Enum.Parse(typeof (BaseInputCharacter.Direction), values[4])
                              };
        }

        public static MapPlayerData[] GetPlayerStartPositions(string mapName)
        {
            var completePath = string.Format("Content/Map/{0}/{1}.player", mapName, mapName);

            List<MapPlayerData> playerData = new List<MapPlayerData>();
            using (var SR = new StreamReader(completePath))
            {
                while (!SR.EndOfStream)
                {
                    playerData.Add(new MapPlayerData(SR.ReadLine()));
                }
            }

            return playerData.ToArray();
        }
    }
}